<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/1
 * Time: 9:33
 */
namespace app\team\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\team\model\Comment as App;
class Console extends Common
{
    public function console()
    {
        //获取待核销订单数
        $no_order = Db::name('gtour_order')->where('is_del', 0)->where('gtour_id',Session::get('team')['team_id'])->where('order_status', 1)->count();
        $this->assign('no_order', $no_order);
        //最近12天的订单业务总额
        $week = $this->get_weeks('');
        $weeks = json_encode($week[0]);
        $zong = json_encode($week[1]);
        $this->assign('week', $weeks);
        $this->assign('zong', $zong);
        //最近12天的订单业务总额
        $week_yes = $this->get_weeks_yes('');
        $heng = json_encode($week_yes[0]);
        $zong1 = json_encode($week_yes[1]);
        $this->assign('heng', $heng);
        $this->assign('zong1', $zong1);
        //获取订单总额
        $where = 'order_status=1 or order_status=3 or order_status=4';
        $all_money = Db::name('gtour_order')->field('order_price')->where('gtour_id',Session::get('team')['team_id'])->where('is_del',0)->where($where)->sum('order_price');
        $this->assign('all_money',$all_money);
        //获取今日的数据
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));

        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $where1['create_time'] = ['between time', [$beginToday,$endToday]];
        $today_money = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where($where1)->where('is_del',0)->where($where)->sum('order_price');
        $this->assign('today_money',$today_money);

        //php获取本月起始时间戳和结束时间戳
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
        $where2['create_time'] = ['between time', [$beginThismonth,$endThismonth]];
        $month_money = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where($where2)->where('is_del',0)->where($where)->sum('order_price');
        $this->assign('month_money',$month_money);

        //实际收益额
        //总
        $sum_fee = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where('is_del',0)->where($where)->sum('commission_fee');
        $all = $all_money-$sum_fee;
        $this->assign('all',$all);
        //本日
        $sum_today_fee = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where($where1)->where('is_del',0)->where($where)->sum('commission_fee');
        $today_fee = $today_money-$sum_today_fee;
        $this->assign('today_fee',$today_fee);
        //本月
        $month_fee =  Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where($where2)->where('is_del',0)->where($where)->sum('commission_fee');
        $month_read = $month_money-$month_fee;
        $this->assign('month_read',$month_read);
        return view();
    }
    //订单月
    public function order_month()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_month()[0],'zong'=>$this->get_month()[1]]);
        }
    }
    //订单日
    public function order_week()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_weeks()[0],'zong'=>$this->get_weeks()[1]]);
        }
    }

    //订单月
    public function order_month_yes()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_month_yes()[0],'zong'=>$this->get_month_yes()[1]]);
        }
    }
    //订单日
    public function order_week_yes()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_weeks_yes()[0],'zong'=>$this->get_weeks_yes()[1]]);
        }
    }
    /**
     * 获取最近七天所有日期
     */
    function get_weeks($time = '', $format='m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        $money = [];
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for ($i=1; $i<=12; $i++){
            $date[] = date($format ,strtotime( '+' . $i-12 .' days', $time));
            $firstday = date('Y-m-d 00:00:00', strtotime( '+' . $i-12 .' days', $time));
            $lastday = date('Y-m-d 23:59:59',strtotime( '+' . $i-12 .' days', $time));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where['create_time'] = ['between time',[$first,$last]];
            $money[] = Db::name('gtour_order')
            ->where('is_del',0)->where($where)
                ->where($where1)->where('gtour_id',Session::get('team')['team_id'])->sum('order_price');
        }
        $times = [$date,$money];
        return $times;
    }

    function get_weeks_yes($time = '', $format='m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        $money = [];
        $money1 = [];
        $money2 = [];
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for ($i=1; $i<=12; $i++){
            $date[] = date($format ,strtotime( '+' . $i-12 .' days', $time));
            $firstday = date('Y-m-d 00:00:00', strtotime( '+' . $i-12 .' days', $time));
            $lastday = date('Y-m-d 23:59:59',strtotime( '+' . $i-12 .' days', $time));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where['create_time'] = ['between time',[$first,$last]];
            $money1[] = Db::name('gtour_order')
                ->where('is_del',0)->where($where)
                ->where($where1)->where('gtour_id',Session::get('team')['team_id'])->sum('order_price');
            $money2[] = Db::name('gtour_order')
                ->where('is_del',0)->where($where)
                ->where($where1)->where('gtour_id',Session::get('team')['team_id'])->sum('commission_fee');
            $money[] = $money1[$i-1]-$money2[$i-1];
        }
        $times = [$date,$money];
        return $times;
    }
    /*
     * 获取最近12个月的数据
     */
    function get_month()
    {
        $dates = [];
        $money = [];
        $currentTime = time();
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("m",$currentTime));
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for($i=0;$i<12;$i++) {
            $nMonth = $cMonth - $i;
            $cyear = $nMonth == 0 ? ($cyear - 1) : $cyear;
            $nMonth = $nMonth <= 0 ? 12 + $nMonth : $nMonth;
            $dates[] = $cyear .'.'. $nMonth;
            $date = $cyear . "-" . $nMonth . "-1";
            $firstday = date('Y-m-01 00:00:00', strtotime($date));
            $lastday = date('Y-m-t 23:59:59', strtotime($date));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where["create_time"] = ['between time',[$first,$last]];
            $money[] = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where('is_del',0)->where($where)->where($where1)->sum('order_price');

        }
        $times = [array_reverse($dates),array_reverse($money)];
        return $times;
    }

    function get_month_yes()
    {
        $dates = [];
        $money = [];
        $money1 = [];
        $money2 = [];
        $currentTime = time();
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("m",$currentTime));
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for($i=0;$i<12;$i++) {
            $nMonth = $cMonth - $i;
            $cyear = $nMonth == 0 ? ($cyear - 1) : $cyear;
            $nMonth = $nMonth <= 0 ? 12 + $nMonth : $nMonth;
            $dates[] = $cyear .'.'. $nMonth;
            $date = $cyear . "-" . $nMonth . "-1";
            $firstday = date('Y-m-01 00:00:00', strtotime($date));
            $lastday = date('Y-m-t 23:59:59', strtotime($date));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where["create_time"] = ['between time',[$first,$last]];
            $money1[] = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money2[] = Db::name('gtour_order')->where('gtour_id',Session::get('team')['team_id'])->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $money[] = $money1[$i]-$money2[$i];

        }
        $times = [array_reverse($dates),array_reverse($money)];
        return $times;
    }
}